﻿using Microsoft.AspNetCore.Mvc;
using MVC_Demo.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using WebApplication1.Models.Database;

namespace WebApplication1.Controllers
{
    public class TestController : Controller
    {

        private readonly McStoreContext _db;
        public TestController(McStoreContext db)
        {
            _db = db;
        }
        [Route("{controller}/{action}")]
        [Route("/api/{controller}/{acion}")]
        public IActionResult Index()
        {
            return View();
        }
        public IActionResult About()
        {
            return View();
        }
        [Route("/myview")]
        public IActionResult myView() { return View(); }


        public IActionResult ViewDataDemo()
        {
            ViewData["name"] = "张三";
            ViewData["date"] = new DateTime(2022, 1, 1);
            return View();
        }



        public IActionResult ViewBagDemo()
        {
            ViewBag.name = "张三";
            ViewBag.date = new DateTime(2022, 1, 1);
            return View();
        }

        public IActionResult ViewModelDemo()
        {
            var person=new Person();
            person.Name = "张三";

            person.Number = 1;
            person.Date = new DateTime(2022, 1, 1);

            return View(person);
        }

        public IActionResult Add()
        {
            var model = new Category() { CateName = "图书" };
            //上下文.表明.操作
            _db.Categories.Add(model);
            //保存更改 执行SQL
            var row = _db.SaveChanges();
            //如果row大于0 表示新增执行成功
            if (row > 0)
            {
                return Content("添加成功");
            }
                return Content("添加失败");
        }

        public IActionResult AddRange()
        {
            var list = new List<Category>()
            {
                new Category
                {
                    CateName="电脑"
                },

                new Category
                {
                    CateName="手机"
                }
            };
            _db.Categories.AddRange(list);
            var row = _db.SaveChanges();
            if (row > 0)
            {
                return Content("添加成功");
            }
            return Content("添加失败");
        }

        public IActionResult AddGoods()
        {
            var model = new Good()
            {
                Name = "Dell G15",
                CateId = 1,
                Cover = "http://dell.com",
                Price = 7999,
                Stock = 1000
            };
            _db.Goods.Add(model);
            var row = _db.SaveChanges();
            if (row > 0)
            {
                return Content("添加成功");
            }
            return Content("添加失败");


        }

        public IActionResult GetGoods()
        {
            var list=_db.Goods.Where(x=>x.CateId==1).ToList();
            return new ObjectResult(list);
        }

        public IActionResult UpdateGoods()
        {
            var model=_db.Goods.FirstOrDefault(x=>x.Name== "Dell G15");
            if (model == null)
            {
                return Content("数据不存在");
            }
            model.CateId = 2;
            _db.Goods.Update(model);
            var row = _db.SaveChanges();
            if(row > 0)
            {
                return Content("修改成功");
            }
            else
            {
                return Content("修改失败");
            }
        }

        public IActionResult DeleteGoods()
        {
            var model = _db.Goods.FirstOrDefault(x => x.Name == "Dell G15");
            if (model == null) return Content("未找到数据");
            _db.Goods.Remove(model);
            var row= _db.SaveChanges();
            if (row > 0)
            {
                return Content("删除成功");
            }
            else
            {
                return Content("删除失败");
            }
        }

        //4.1 查询ID为2，或者价格大于2000的商品
        public IActionResult SelectGoods()
        {
            var list = _db.Goods.Where(x => x.CateId==2 && x.Price>2000);
            if (list == null) return Content("未找到数据");
            var row = _db.SaveChanges();
            if (row > 0)
            {
                return Content("删除成功");
            }
            else
            {
                return Content("删除失败");
            }
        }

        public IActionResult SortDemo()
        {
            //顺序
            var list1 = _db.Categories.Where(x => x.Id > 0).OrderBy(x => x.Id).ToList();
            //倒序
            var list2 = _db.Categories.Where(x => x.Id > 0).OrderByDescending(x => x.Id).ToList();
            return new ObjectResult(list1);
        }


        public IActionResult SoftGoods()
        {
            var list=_db.Goods.OrderByDescending(x => x.CateId).ToList();
            return new ObjectResult(list);
        }

        public IActionResult SkipGoods()
        {
            var list=_db.Goods.OrderByDescending(x => x.Price).Skip(2).Take(5).ToList();
            return new ObjectResult(list);

        }

        public IActionResult AnyGoods()
        {
            return _db.Goods.Any(x => x.Price == 100) ? Content("存在") : Content("不存在");
        }

        public IActionResult CountGoods()
        {
            return Content(_db.Goods.Count(x => x.Price >= 500 && x.Price <= 1000).ToString());
        }

        public IActionResult SumGoods()
        {
            return Content("商品总额："+_db.Goods.Where(x=>x.CateId == 3).Sum(x => x.Price).ToString());
        }

        public IActionResult StockGoods()
        {
            return new ObjectResult(_db.Goods.GroupBy(x => x.CateId).Select(x => new
            {
                x.Key,
                Sum = x.Sum(y => y.Stock)
            }));
        }

        public IActionResult JoinGoods()
        {
            var list=_db.Goods.Join(_db.Categories, x => x.CateId, y => y.Id, (x, y) => new
            {
                x.Id,
                x.Name,
                x.Cover,
                x.CreateTime,
                y.CateName
            }).ToList();
            return new ObjectResult(list);
        }
    }
}
